﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
using System.Collections;


namespace MDA
{

    /// <summary>
    /// 对页面进行控制的代码
    /// </summary>
    public class PageStyle
    {
        /// <summary>
        /// 用来自定义页面状态的一个变量                      
        /// </summary>
        public static int PageStatus = 0;
        public PageStyle()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public static Hashtable GetQueryString(string qs)
        {
            if (qs.Equals(""))
                return null;
            Hashtable ht = new Hashtable();
            string _qs = HttpContext.Current.Server.UrlDecode(qs);
            string[] param = _qs.Split('&');
            foreach (string p in param)
            {
                int index = p.IndexOf("=");
                string key = p.Substring(0, index);
                string value = p.Substring(index + 1, p.Length - index - 1);
                ht.Add(key, value);
            }
            return ht;
        }


        /// <summary>
        /// 清除所有文本框的内容                    
        /// </summary>
        public static void ClearText(Control p)
        {
            foreach (Control c in p.Controls)
            {
                if (c is TextBox)
                {
                    TextBox tt = c as TextBox;
                    tt.Text = "";
                }
            }
        }

        /// <summary>
        /// 绑定DropDownList    
        /// 带空记录     
        /// </summary>
        /// <param name="ddl"></param>
        /// <param name="tablename"></param>
        /// <param name="textfield"></param>
        /// <param name="valuefield"></param>
        public static void BindDdl(DropDownList ddl, string tablename, string textfield, string valuefield)
        {
            ddl.Items.Clear();
            String sql = "select  DISTINCT " + textfield + " , " + valuefield + "  from " + tablename;
            SqlConnection con = new SqlConnection(ConnectData.ConnectString);
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            ddl.Items.Add(new ListItem("", ""));
            foreach (DataRow dr in dt.Rows)
            {
                ddl.Items.Add(new ListItem(dr[textfield].ToString().Trim(), dr[valuefield].ToString().Trim()));
            }
        }

        /// <summary>
        /// 绑定DropDownList,带条件 
        /// 带空记录     
        /// </summary>
        /// <param name="ddl"></param>
        /// <param name="tablename"></param>
        /// <param name="textfield"></param>
        /// <param name="valuefield"></param>
        public static void BindDdl_Plus(DropDownList ddl, string tablename, string textfield, string valuefield, string condition)
        {
            ddl.Items.Clear();
            String sql = "select  DISTINCT " + textfield + " , " + valuefield + "  from " + tablename + " where " + condition;
            SqlConnection con = new SqlConnection(ConnectData.ConnectString);
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            ddl.Items.Add(new ListItem("", ""));
            foreach (DataRow dr in dt.Rows)
            {
                ddl.Items.Add(new ListItem(dr[textfield].ToString().Trim(), dr[valuefield].ToString().Trim()));
            }
        }


        /// <summary>
        /// 从DataList中找到指定控件名的控件。                       
        /// </summary>
        /// <param name="Parent">父控件</param>
        /// <param name="controlName">子控件名称</param>
        /// <returns>返回找到的控件</returns>
        public static Control FindChildControl(Control parent, string controlName)
        {
            foreach (Control c in parent.Controls)
            {
                if (c.ID == controlName)
                {
                    return c;
                }
                else if (c.HasControls())
                {
                    Control cc = FindChildControl(c, controlName);
                    if (cc != null) return cc;
                }
            }
            return null;
        }

        /// <summary>
        /// 关闭页面。                             
        /// </summary>
        /// <param name="p">将被关闭的页面。</param>
        public static void CloseWin(Page p)
        {
            RegisterStartScript(p, "window.close();");
        }

        /// <summary>
        /// 用window.showModalDialog()显示模式窗口（弹出式窗口）。   
        /// </summary>
        /// <param name="SourcePage">源页面。在其上弹出窗口。</param>
        /// <param name="args">参数。包括目标页面、对话框参数、窗体参数。如：高度，宽度等信息。</param>
        public static void ShowModuleWin(Page SourcePage, MWinArgs args)
        {
            if (args.Url == null || args.Url == "")
            {
                throw new ArgumentException("参数中的Url没有初始化。", "Url");
                return;
            }
            string script = "window.showModalDialog('" + args.Url + "','" + args.DialogArguments + "','" + args.Features.ToString() + "')";
            if (!SourcePage.ClientScript.IsStartupScriptRegistered("smd11"))
            {
                SourcePage.ClientScript.RegisterStartupScript(SourcePage.GetType(), "smd11", script, true);
            }
        }


        /// <summary>
        /// 用window.open()打开一个新窗口.
        /// </summary>
        /// <param name="SourcePage">源页面。在其上打开窗口。</param>
        /// <param name="args">打开页面的参数。包括打开页面，目标框架，样式等。</param>
        public static void ShowOpenWin(Page SourcePage, WinOpenArgs args)
        {
            if (args.Url == null || args.Url == "")
            {
                throw new ArgumentException("参数中的Url没有初始化。", "Url");
                return;
            }
            string script = "window.open('" + args.Url + "','" + args.UTarget.ToString() + "','" + args.Features.ToString() + "'," + args.Replace.ToString().ToLower() + ");";
            if (!SourcePage.ClientScript.IsStartupScriptRegistered("ow"))
            {
                SourcePage.ClientScript.RegisterStartupScript(SourcePage.GetType(), "ow", script, true);
            }
        }

        /// <summary>
        /// 在指定页面注册启动脚本
        /// </summary>
        /// <param name="SourcePage">源页面。</param>
        /// <param name="script">将注册的启动脚本</param>
        public static void RegisterStartScript(Page SourcePage, string script)
        {
            if (!SourcePage.ClientScript.IsStartupScriptRegistered(SourcePage.GetType(), "sptstart"))
            {
                SourcePage.ClientScript.RegisterStartupScript(SourcePage.GetType(), "sptstart", script, true);
            }
        }

        /// <summary>
        /// 在指定页面注册启动脚本   
        /// </summary>
        /// <param name="SourcePage">源页面。</param>
        /// <param name="script">将注册的启动脚本</param>
        public static void RegisterStartScript(Page SourcePage, string script, string key)
        {
            if (!SourcePage.ClientScript.IsStartupScriptRegistered(SourcePage.GetType(), key))
            {
                SourcePage.ClientScript.RegisterStartupScript(SourcePage.GetType(), key, script, true);
            }
        }

        /// <summary>
        /// 在指定的页面上注册脚本块。             
        /// </summary>
        /// <param name="SourcePage"></param>
        /// <param name="script"></param>
        public static void RegisterBlockScript(Page SourcePage, string script)
        {
            if (!SourcePage.ClientScript.IsClientScriptBlockRegistered("sptblock"))
            {
                SourcePage.ClientScript.RegisterClientScriptBlock(SourcePage.GetType(), "sptblock", script, true);
            }
        }
        /// <summary>
        /// 在指定的页面上注册脚本块。包括关键字段        
        /// </summary>
        /// <param name="SourcePage"></param>
        /// <param name="script"></param>
        public static void RegisterBlockScript(Page SourcePage, string script, string key)
        {
            if (!SourcePage.ClientScript.IsClientScriptBlockRegistered(key))
            {
                SourcePage.ClientScript.RegisterClientScriptBlock(SourcePage.GetType(), key, script, true);
            }
        }

        /// <summary>
        /// 在指定的页面上注册标记块。                    
        /// </summary>
        /// <param name="SourcePage"></param>
        /// <param name="mark"></param>
        public static void RegisterBlockMark(Page SourcePage, string mark)
        {
            if (!SourcePage.ClientScript.IsClientScriptBlockRegistered("sptmark"))
            {
                SourcePage.ClientScript.RegisterClientScriptBlock(SourcePage.GetType(), "sptmark", mark, false);
            }
        }

        /// <summary>
        /// 在指定的页面上注册标记块。                         
        /// </summary>
        /// <param name="SourcePage"></param>
        /// <param name="mark"></param>
        public static void RegisterBlockMark(Page SourcePage, string mark, string key)
        {
            if (!SourcePage.ClientScript.IsClientScriptBlockRegistered(key))
            {
                SourcePage.ClientScript.RegisterClientScriptBlock(SourcePage.GetType(), key, mark, false);
            }
        }


        /// <summary>
        /// 给用户弹出一个警告框，显示提示信息。                 
        /// </summary>
        /// <param name="p">显示提示信息的页面</param>
        /// <param name="text">提示内容</param>
        public static void Show(System.Web.UI.Page p, string text)
        {
            //if (text == "") return;
            //StringBuilder sb = new StringBuilder();
            //sb.Append("window.alert('" + text + "');");
            //if (!p.ClientScript.IsStartupScriptRegistered("client"))
            //{
            //    p.ClientScript.RegisterStartupScript(p.GetType(), "client", sb.ToString(),true);
            //}
            string _text = string.Format("window.alert('{0}');", text);
            RegisterStartScript(p, _text);
        }



        /// 
        /// 清空浏览器客户端的缓存
        /// 
        public static void ClearClientPageCache()
        {
            HttpContext.Current.Response.Buffer = false;
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.AddHeader("pragma", "no-cache");
            HttpContext.Current.Response.AddHeader("cache-control", "private");
            HttpContext.Current.Response.CacheControl = "no-cache";
        }

    }
}